using Ln.Fram.Ddd.Application;
using Ln.Fram.SqlSugarCore.Abstractions;
using Ln.Rbac.Application.Contracts.Dtos.DictionaryType;
using Ln.Rbac.Application.Contracts.IServices;
using Ln.Rbac.Domain.Entities;
using Ln.Rbac.Domain.Shared.Consts;
using SqlSugar;
using Volo.Abp.Application.Dtos;

namespace Ln.Rbac.Application.Services;

/// <summary>
/// DictionaryType服务实现
/// </summary>
public class DictionaryTypeService : LnCrudAppService<DictionaryTypeAggregateRoot, DictionaryTypeGetOutputDto, DictionaryTypeGetListOutputDto, Guid, DictionaryTypeGetListInputVo, DictionaryTypeCreateInputVo, DictionaryTypeUpdateInputVo>, IDictionaryTypeService
{
	private ISqlSugarRepository<DictionaryTypeAggregateRoot, Guid> _repository;

	public DictionaryTypeService(ISqlSugarRepository<DictionaryTypeAggregateRoot, Guid> repository) : base(repository)
	{
		_repository = repository;
	}

	public override async Task<PagedResultDto<DictionaryTypeGetListOutputDto>> GetListAsync(DictionaryTypeGetListInputVo input)
	{
		RefAsync<int> total = 0;
		var entities = await _repository._DbQueryable.WhereIF(input.DictName is not null, x => x.DictName.Contains(input.DictName!))
			.WhereIF(input.DictType is not null, x => x.DictType!.Contains(input.DictType!))
			.WhereIF(input.State is not null, x => x.State == input.State)
			.WhereIF(input.StartTime is not null && input.EndTime is not null, x => x.CreationTime >= input.StartTime && x.CreationTime <= input.EndTime)
			.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
		return new PagedResultDto<DictionaryTypeGetListOutputDto>
		{
			TotalCount = total,
			Items = await MapToGetListOutputDtosAsync(entities)
		};
	}

	protected override async Task CheckCreateInputDtoAsync(DictionaryTypeCreateInputVo input)
	{
		var isExist = await _repository.IsAnyAsync(x => x.DictType == input.DictType);
		if (isExist)
			throw new UserFriendlyException(DictionaryConst.Exist);
	}

	protected override async Task CheckUpdateInputDtoAsync(DictionaryTypeAggregateRoot entity, DictionaryTypeUpdateInputVo input)
	{
		var isExist = await _repository._DbQueryable.Where(x => x.Id != entity.Id).AnyAsync(x => x.DictType == input.DictType);
		if (isExist)
			throw new UserFriendlyException(DictionaryConst.Exist);
	}
}
